var main = angular.module('main');

main.controller('add_projectCtrl', ['$scope', 'http', '$state', function($scope, http, $state) {
    $scope.projectName = "";
    $scope.projectDesc = "";
    $scope.projectNameError = false;
    $scope.projectNameErrorMsg = "";
    $scope.projectDescError = false;
    $scope.projectDescErrorMsg = "";

    $scope.checkProjectName = function() {
        if ($scope.projectName.length > 20) {
            $scope.projectNameError = true;
            $scope.projectNameErrorMsg = "项目名称长度不得多于20字";
            return false;
        }
        if (!$scope.projectName.length) {
            $scope.projectNameError = true;
            $scope.projectNameErrorMsg = "请填写项目名称";
            return false;
        }
        $scope.projectNameError = false;
        return true;
    };
    $scope.checkProjectDesc = function() {
        if ($scope.projectDesc.length > 500) {
            $scope.projectDescError = true;
            $scope.projectDescErrorMsg = "项目描述长度不得多于500字";
            return false;
        }
        $scope.projectDescError = false;
        return true;
    };

    $scope.submit = function() {
        console.log("in submit");
        if ($scope.checkProjectName() && $scope.checkProjectDesc()) {
            http.post('/project/add', { name: $scope.projectName, description: $scope.projectDesc }).then(function(data) {
                if (data.code === 0) {
                    $state.go('project.edit_project', { project_id: data.data.id });
                }
                if (data.code === 1) {
                    $scope.projectNameError = true;
                    $scope.projectNameErrorMsg = "该项目名称已被使用";
                }
            });
        }
    };
}]);